/**
 * x组件的基本操作
 */
export default {
  computed: {
    xif() {
      return this.getXMeta('xif') !== false
    },
    xcode() {
      return this.getXMeta('xcode')
    },
    xtype() {
      return this.getXMeta('xtype')
    },
  },
  methods: {
    getParentX(name = undefined) {
      let parent = this.$parent;
      while (parent) {
        if (parent.$options && parent.xcode !== name) {
          parent = parent.$parent;
        } else {
          return parent;
        }
      }
      return false;
    },
    getXChildrenMetas() {
      return this.getXMeta('xcomponents')
    },
    getXChildMetaPath(index, xchildrenPath='xcomponents') {
      return this.xmetapath+'.'+xchildrenPath+'.'+index
    },
    getXChildComponent() {
      return this.getXMeta('xcomponent')
    },

    // ===========================
    // 事件
    // ===========================

    /**
     * 调用所属页面的方法
     */
    invokeHostMethod(name, ...args) {
      let host = this.getHost()
      let method = host&&host[name]
      return method&&method(...args)
    },
    /**
     * 调用xone 根组件的方法
     */
    invokeXOneMethod(name, ...args) {
      let xone = this.getXOne()
      let method = xone&&xone[name]
      return method&&method(...args)
    },
    /**
     * 调用页面元数据的事件
     */
    emitHostXCallBack(eventName, ...args) {
      let func = this.getXMeta(eventName, {xbase: '', isRaw: true})
      return func&&func(...args) 
    },
    /**
     * 调用组件元数据的事件
     */
    emitMyXCallBack(eventName, ...args) {
      let func = this.getXMeta(eventName, {isRaw: true})
      return func&&func(...args) 
    },
    /**
     * 调用组件下子组件元数据的事件
     */
    emitChildXCallBack(eventName, item, ...args) {
      let func = this.getXPath(item, eventName)
      return func&&func(...args) 
    }
  }
}
